# -*- coding:utf-8 -*-
"""Created By Corvo"""


class Register(object):
    """注册器"""

    def __init__(self):
        super(Register, self).__init__()
        self._storage = {}

    def __call__(self, target, *args, **kwargs):
        return self.register(target)

    def register(self, target):
        def trigger(key, value):
            if not callable(value):
                raise Exception("function must be callable: %s" % value)
            if key in self._storage:
                print "[warn]", "override register function: %s" % value
            self._storage[key] = value
            return value

        if callable(target):
            return trigger(target.__name__, target)
        return lambda func: trigger(target, func)
